﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Web
{
    public partial class service : System.Web.UI.Page
    {
        private int CustomerNowpage = 1;
        private const int CustomerPAGESIZE = 20;
        private int CustomerPageCount = ServiceInfoLogic.GetPageCount(CustomerPAGESIZE, 1);

        private int PartnerNowpage = 1;
        private const int PartnerPAGESIZE = 20;
        private int PartnerPageCount = ServiceInfoLogic.GetPageCount(PartnerPAGESIZE, 2);

        private int WorksCategoryNowPage = 1;
        private const int WORKSCATEGORYPAGESIZE = 4;
        private int WorksCategoryPageCount = WorksCategoryInfoLogic.GetPageCount(WORKSCATEGORYPAGESIZE);

        

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "服务";

            if (!IsPostBack)
            {

                WorksCategoryBindData(WORKSCATEGORYPAGESIZE, WorksCategoryNowPage);
                CustomerBindData(CustomerNowpage);
                PartnerDataBind(PartnerNowpage);
            }
        }

        public void CustomerBindData(int nowpage)
        {
            List<ServiceInfo> customerlist = ServiceInfoLogic.GetCategoryServiceInfo(1, CustomerPAGESIZE, nowpage);
            if (customerlist == null)
            {
                customerlist = new List<ServiceInfo>();
                customerlist.Add(new ServiceInfo());
            }

            CustomerList.DataSource = customerlist;
            CustomerList.DataBind();
            
            if (CustomerPageCount > 1)
            {
                if (nowpage == 1)
                {
                    btn_Customer_Pre.ImageUrl = "~/images/bnp.png";
                    btn_Customer_Next.ImageUrl = "~/images/wnn.png";
                    btn_Customer_Pre.Enabled = false;
                    btn_Customer_Next.Enabled = true;
                }
                else if (nowpage == CustomerPageCount)
                {
                    btn_Customer_Pre.ImageUrl = "~/images/wnp.png";
                    btn_Customer_Next.ImageUrl = "~/images/bnn.png";
                    btn_Customer_Pre.Enabled = true;
                    btn_Customer_Next.Enabled = false;
                }
                else
                {
                    btn_Customer_Pre.ImageUrl = "~/images/wnp.png";
                    btn_Customer_Next.ImageUrl = "~/images/wnn.png";
                    btn_Customer_Pre.Enabled = true;
                    btn_Customer_Next.Enabled = true;
                }
                btn_Customer_Pre.Visible = true;
                btn_Customer_Next.Visible = true;
            }
            else
            {
                btn_Customer_Pre.Visible = false;
                btn_Customer_Next.Visible = false;
            }
        }

        public void PartnerDataBind(int nowpage)
        {
            List<ServiceInfo> partnerlist = ServiceInfoLogic.GetCategoryServiceInfo(2, PartnerPAGESIZE, nowpage);
            if (partnerlist == null)
            {
                partnerlist = new List<ServiceInfo>();
                partnerlist.Add(new ServiceInfo());
            }
            PartnerList.DataSource = partnerlist;
            PartnerList.DataBind();

            if (PartnerPageCount > 1)
            {
                if (nowpage == 1)
                {

                    btn_Partner_Pre.ImageUrl = "~/images/bnp.png";
                    btn_Partner_Next.ImageUrl = "~/images/wnn.png";
                    btn_Partner_Pre.Enabled = false;
                    btn_Partner_Next.Enabled = true;
                }
                else if (nowpage == PartnerPageCount)
                {
                    btn_Partner_Pre.ImageUrl = "~/images/wnp.png";
                    btn_Partner_Next.ImageUrl = "~/images/bnn.png";
                    btn_Partner_Pre.Enabled = true;
                    btn_Partner_Next.Enabled = false;
                }
                else
                {
                    btn_Partner_Pre.ImageUrl = "~/images/wnp.png";
                    btn_Partner_Next.ImageUrl = "~/images/wnn.png";
                    btn_Partner_Pre.Enabled = true;
                    btn_Partner_Next.Enabled = true;
                }
                btn_Partner_Pre.Visible = true;
                btn_Partner_Next.Visible = true;
            }
            else
            {
                btn_Partner_Pre.Visible = false;
                btn_Partner_Next.Visible = false;
            }
        }

        public void WorksCategoryBindData(int pagesize, int nowpage)
        {
            List<WorksCategoryInfo> list = WorksCategoryInfoLogic.GetNowPageWorksCategoryInfo(pagesize, nowpage);
            if (list == null)
            {
                list = new List<WorksCategoryInfo>();
                list.Add(new WorksCategoryInfo());
            }
            WorksCategoryData.DataSource = list;
            WorksCategoryData.DataBind();
            if (WorksCategoryPageCount > 1)
            {
                if (nowpage == 1)
                {
                    btn_pre.ImageUrl = "~/images/bnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = false;
                    btn_next.Enabled = true;
                }
                else if (nowpage == WorksCategoryPageCount)
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/bnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = false;
                }
                else
                {
                    btn_pre.ImageUrl = "~/images/wnp.png";
                    btn_next.ImageUrl = "~/images/wnn.png";
                    btn_pre.Enabled = true;
                    btn_next.Enabled = true;
                }
                btn_pre.Visible = true;
                btn_next.Visible = true;
            }
            else
            {
                btn_pre.Visible = false;
                btn_next.Visible = false;
            }
        }
        
        protected void btn_Customer_Pre_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                CustomerNowpage = Int32.Parse(lbl_Customer_NowPage.Text.Trim());
            }
            catch
            {
                CustomerNowpage = 1;
            }
            CustomerNowpage--;
            if (CustomerNowpage >= 1)
            {
                lbl_Customer_NowPage.Text = CustomerNowpage.ToString();
                CustomerBindData(CustomerNowpage);
            }
        }
    
        protected void btn_Customer_Next_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                CustomerNowpage = Int32.Parse(lbl_Customer_NowPage.Text.Trim());
            }
            catch
            {
                CustomerNowpage = 1;
            }
            CustomerNowpage++;
            if (CustomerNowpage <= PartnerPageCount)
            {
                lbl_Customer_NowPage.Text = CustomerNowpage.ToString();
                CustomerBindData(CustomerNowpage);
            }
        }

        protected void btn_pre_Click(object sender, ImageClickEventArgs e)
        {
            
            try
            {
                WorksCategoryNowPage = Int32.Parse(lbl_WorksCategory_Nowpage.Text.Trim());
            }
            catch
            {
                WorksCategoryNowPage = 1;
            }
            WorksCategoryNowPage--;
            if (WorksCategoryNowPage >= 1)
            {
                lbl_WorksCategory_Nowpage.Text = WorksCategoryNowPage.ToString();
                WorksCategoryBindData(WORKSCATEGORYPAGESIZE, WorksCategoryNowPage);
            }
        }

        protected void btn_next_Click(object sender, ImageClickEventArgs e)
        {   
            try
            {
                WorksCategoryNowPage = Int32.Parse(lbl_WorksCategory_Nowpage.Text.Trim());
            }
            catch
            {
                WorksCategoryNowPage = 1;
            }
            WorksCategoryNowPage++;
            if (WorksCategoryNowPage <= WorksCategoryPageCount)
            {
                lbl_WorksCategory_Nowpage.Text = WorksCategoryNowPage.ToString();
                WorksCategoryBindData(WORKSCATEGORYPAGESIZE, WorksCategoryNowPage);
            }
             
        }

        protected void btn_Partner_Pre_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                PartnerNowpage = Int32.Parse(lbl_Partner_NowPage.Text.Trim());
            }
            catch
            {
                PartnerNowpage = 1;
            }
            PartnerNowpage--;
            if (PartnerNowpage >= 1)
            {
                lbl_Partner_NowPage.Text = PartnerNowpage.ToString();
                PartnerDataBind(PartnerNowpage);
            }
        }

        protected void btn_Partner_Next_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                PartnerNowpage = Int32.Parse(lbl_Partner_NowPage.Text.Trim());
            }
            catch
            {
                PartnerNowpage = 1;
            }
            PartnerNowpage++;
            if (PartnerNowpage <= PartnerPageCount)
            {
                lbl_Partner_NowPage.Text = PartnerNowpage.ToString();
                PartnerDataBind(PartnerNowpage);
            }
        }
    }
}
